#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/4/26 下午11:00
# @Author  : zhangzhen
# @Site    : 
# @File    : w2v.py
# @Software: PyCharm
from gensim import corpora, models, similarities

class w2v:

    @staticmethod
    def train(root, name, target):
        corpus = []
        for line in open(root+name):
            corpus.append(line.strip().split())
        model = models.Word2Vec(corpus, size=100, window=5, min_count=5, workers=4)
        model.save(root+target)
        pass


if __name__ == '__main__':
    print '主函数'
    # w2v.train('../../data/', 'w2v.dat', 'w2v.model')

    stop_word_path = '../../data/stopwords.txt'
    # 加载停用词
    stopList = [line.strip().decode('utf-8') for line in open(stop_word_path).readlines()]
    cent = []
    for i in range(7):
        tmp = []
        for line in open('../../data/cent_' + str(i) + '.txt'):
            text = line.strip()
            tmp.append(text.split('\t')[2])
        cent.append(tmp)
    w2v = models.Word2Vec.load('../../data/w2v.model')

    for c in cent:
        for sent in c:
            for w in sent.split(' '):
                try:
                    print w2v.vocab[w]
                except KeyError, e:
                    pass

    pass
